clear,clc;
clear all;
numIter =10;
conf = 3;
confidence_percent = conf;

path_origdata = '/home/liang/Projects/ASD_QC/DataProcessed/rest';
ListPath = '/home/liang/Projects/ASD_QC/Lists';
outputDIR = '/home/liang/Projects/ASD_QC/Results';
templateDIR = '/home/liang/Projects/ASD_QC/Parcellation_template';
resultDIR = '/home/liang/Projects/ASD_QC/Results';

data_type = 'ASD'
symbol = 'OHSU'

SIDs = textread([ListPath '/variability_list_' data_type '_' symbol '.txt'],'%s');

%%%%%%%%%%%%%%%%%%%%
eval(['!mkdir ' outputDIR '/Variability']);

total_lh = [];
total_rh = [];

exclude_lh_template = [templateDIR '/lh_network_1_asym_fs4.mgh']
exclude_rh_template = [templateDIR '/rh_network_1_asym_fs4.mgh']
[lh_vol, M, mr_parms, volsz] = load_mgh(exclude_lh_template);
[rh_vol, M, mr_parms, volsz] = load_mgh(exclude_rh_template);

for n = 1:3
    res = [];

    sess_id = ['00' num2str(n)];
    for s = 1:length(SIDs)
        fprintf(['[step]: ' num2str(s) ' start \n']);

        sid = SIDs{s};

        src_sess_files = dir([resultDIR '/FC_mats/' symbol '/' sid '_bld' sess_id '*']);

        if length(src_sess_files) ~= 1
            continue
        end

        src_sess_data = load([resultDIR '/FC_mats/' symbol '/' src_sess_files(1).name])

        for v = 1:length(SIDs)
            tar_sid = SIDs{v};
            if s ~= v
                des_sess_files = dir([resultDIR '/FC_mats/' symbol '/' tar_sid '_bld' sess_id '*']);

                if length(des_sess_files) ~= 1
                    continue
                end

                des_sess_data = load([resultDIR '/FC_mats/' symbol '/' des_sess_files(1).name]);

                % src_sess_data_fc = 0.5 * log((1 + src_sess_data.fc) ./ (1 - src_sess_data.fc));
                % des_sess_data_fc = 0.5 * log((1 + des_sess_data.fc) ./ (1 - des_sess_data.fc));
                C_res = func_matcorr(src_sess_data.fc, des_sess_data.fc);

                % C_res(isnan(C_res)) = 0.0;

                res = [res; C_res];
            end
        end

        fprintf(['[step]: ' num2str(s) ' finished \n']);
    end

    res = nanmean(res, 1);

    res = 1 - res;
    res(isnan(res)) = 0.0;
    fprintf('done');


    lh = res(:, 1:2562)'
    rh = res(:, 2563:5124)'

    save([outputDIR '/Variability/inter_Variability_FS4_' data_type '_' symbol '_bld00' num2str(n) '.mat'], 'lh', 'rh');

    tem_lh = lh;
    tem_rh = rh;

    total_lh = [total_lh lh];
    total_rh = [total_rh rh];

    tem_lh(find(lh_vol == 1)) = 0.0;
    tem_rh(find(rh_vol == 1)) = 0.0;

    save_mgh(tem_lh, [outputDIR '/Variability/' 'lh_' data_type '_' symbol '_bld00' num2str(n) '_fs4_inter_variability.mgh'], eye(4));
    save_mgh(tem_rh, [outputDIR '/Variability/' 'rh_' data_type '_' symbol '_bld00' num2str(n) '_fs4_inter_variability.mgh'], eye(4));
end

lh = total_lh;
rh = total_rh;
lh = mean(lh, 2);
rh = mean(rh, 2);
save([outputDIR '/Variability/inter_Variability_FS4_' data_type '_' symbol '.mat'], 'lh', 'rh');

tem_lh = lh;
tem_rh = rh;
tem_lh(find(lh_vol == 1)) = 0.0;
tem_rh(find(rh_vol == 1)) = 0.0;

save_mgh(tem_lh, [outputDIR '/Variability/' 'lh_' data_type '_' symbol '_fs4_inter_variability.mgh'], eye(4));
save_mgh(tem_rh, [outputDIR '/Variability/' 'rh_' data_type '_' symbol '_fs4_inter_variability.mgh'], eye(4));
